class T {
public:
	bool operator()(vector<int>& v1, vector<int>& v2) {
		int pos = 0;
		cout << endl;
		int n = v1.size();
		while (pos < n) {
			if (v1[pos] == v2[pos]) {
				pos++;
			}
			else {
				return v1[pos] > v2[pos];

			}
		}
		return false;
	}
};
class Solution {
public:
	string rankTeams(vector<string> votes) {
		int n = votes.size(), m = votes[0].size();
		vector<vector<int>>vv(26, vector<int>(m + 1));
		for (auto& str : votes) {
			for (int i = 0; i < m; i++) {
				vv[(int)str[i] - 'A'][i]++;
			}
		}
		for (int i = 0; i < m; i++) {
			vv[votes[0][i] - 'A'][m] = 'Z' + 1 - votes[0][i];
		}
		// for(int i = 0;i < 26;i++){
		//     for(int j = 0;j <= m;j++){
		//         cout << vv[i][j]<<' ';
		//     }
		//     cout<< endl;
		// }
		sort(vv.begin(), vv.end(), T());
		// for (int i = 0; i < 26; i++) {
		// 	for (int j = 0; j <= m; j++) {
		// 		cout << vv[i][j] << ' ';
		// 	}
		// 	cout << endl;
		// }
        string ans;
        for(auto&e:vv){
            if(e[m] == 0) break;
            ans.push_back('Z' + 1 - e[m]);
        }
		return ans;
	}
};
